﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.DataAccess.DBAccess;

namespace CapstoneProjectAEReader.GUIs.LibraryGUI
{
    /// <summary>
    /// Interaction logic for GroupList.xaml
    /// </summary>
    public partial class GroupList : Window
    {
        #region Attributes

        private List<EbookGroup> _groups =new List<EbookGroup>();
        private List<string> _selectedGroups = new List<string>(); 
        private const string Cancel = "cancelButton";
        private const string Done = "done";
        private const string Group = "groupList";
        private List<EbookGroup> _selectedItems = new List<EbookGroup>();
        private DAO _dao;
        #endregion

        public GroupList(List<EbookGroup> groups, DTO dto, List<string> selectedGroup)
        {
            _groups = groups;
            _dao = new DAO(dto);
            _selectedGroups = selectedGroup;
            InitializeComponent();
            ReloadFormData();
            ReloadFormLanguage();
        }

        public void ReloadFormLanguage()
        {
            lbCancel.Content = ResourceReader.GetResourceContent(Cancel);
            lbDone.Content = ResourceReader.GetResourceContent(Done);
            lbGroup.Content = ResourceReader.GetResourceContent(Group);
        }

        public void ReloadFormData()
        {
            _groups.Remove(_groups.Find(e => e.Groupname == Constants.UnGroup));
            GroupListBox.ItemsSource = _groups;
        }

        private void BindFormData()
        {
            foreach (var item in GroupListBox.Items)
            {
                ListBoxItem myListBoxItem = (ListBoxItem)(GroupListBox.ItemContainerGenerator.ContainerFromItem(item));

                // Getting the ContentPresenter of myListBoxItem
                ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

                // Finding textBlock from the DataTemplate that is set on that ContentPresenter
                DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
                CheckBox myCheckBox = (CheckBox)myDataTemplate.FindName("myCheckBox", myContentPresenter);
                if (_selectedGroups.Contains(myCheckBox.Content))
                {
                    myCheckBox.IsChecked = true;
                }
            }
        }

        private void HandleBtDone(object sender, RoutedEventArgs e)
        {
            _selectedItems = GetCheckedItem();
            DialogResult = true;
        }

        private void HandleBtCancel(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        public List<EbookGroup> SelectedItems
        {
            get { return _selectedItems; }
            set { _selectedItems = value; }
        }

        private List<EbookGroup> GetCheckedItem()
        {
            List<EbookGroup> rtList = new List<EbookGroup>();
            // Getting the currently selected ListBoxItem 
            // Note that the ListBox must have 
            // IsSynchronizedWithCurrentItem set to True for this to work
            foreach (var item in GroupListBox.Items)
            {
                ListBoxItem myListBoxItem = (ListBoxItem)(GroupListBox.ItemContainerGenerator.ContainerFromItem(item));

                // Getting the ContentPresenter of myListBoxItem
                ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

                // Finding textBlock from the DataTemplate that is set on that ContentPresenter
                DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
                CheckBox myCheckBox = (CheckBox)myDataTemplate.FindName("myCheckBox", myContentPresenter);
                if (myCheckBox.IsChecked != null && myCheckBox.IsChecked == true)
                {
                    string content = myCheckBox.Content.ToString();
                    EbookGroup temp = _dao.Dto.EbookGroup.Find(e => e.Groupname == content);
                    rtList.Add(temp);
                }
            }
            return rtList;
        }

        private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            BindFormData();
        }

    }
}
